﻿using HW.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;

namespace HW.Database.SQLServer.Repository
{
    public class RotorInformationRepository : IRotorInformationRepository
    {
        HW_testEntities db = new HW_testEntities();

        public List<RotorInformation> GetByConditions(DateTime StartDate, DateTime EndDate, string FuzzyRotorSearch, string RotorNumberSearch, int pageNumber, int pageSize, bool isAscending)
        {
            try
            {
                var query = db.RotorInformation
                .Where(t => t.CreateDateTime >= StartDate && t.CreateDateTime <= EndDate);

                if (!string.IsNullOrEmpty(FuzzyRotorSearch))
                {
                    query = query.Where(t => t.NumberOfJobs.Contains(FuzzyRotorSearch));
                }

                if (!string.IsNullOrEmpty(RotorNumberSearch))
                {
                    query = query.Where(t => t.ProductNumber.Contains(RotorNumberSearch));
                }

                if (isAscending)
                {
                    query = query.OrderBy(t => t.CreateDateTime);
                }
                else
                {
                    query = query.OrderByDescending(t => t.CreateDateTime);
                }

                var result = query.Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList();

                return result;
            }
            catch (Exception ex)
            {
                Logs.LogError(ex);
                return null;
            }

        }

        public int CountByConditions(DateTime StartDate, DateTime EndDate, string FuzzyRotorSearch, string RotorNumberSearch)
        {
            try
            {
                var query = db.RotorInformation
                .Where(t => t.CreateDateTime >= StartDate && t.CreateDateTime <= EndDate);

                if (!string.IsNullOrEmpty(FuzzyRotorSearch))
                {
                    query = query.Where(t => t.NumberOfJobs.Contains(FuzzyRotorSearch));
                }

                if (!string.IsNullOrEmpty(RotorNumberSearch))
                {
                    query = query.Where(t => t.ProductNumber.Contains(RotorNumberSearch));
                }

                var result = query.Count();

                return result;
            }
            catch (Exception ex)
            {

                Logs.LogError(ex);
                return 0;
            }
            

        }

        public List<RotorInformation> ExportByConditions(DateTime StartDate, DateTime EndDate, string FuzzyRotorSearch, string RotorNumberSearch)
        {
            try
            {
                var query = db.RotorInformation
                .Where(t => t.CreateDateTime >= StartDate && t.CreateDateTime <= EndDate);

                if (!string.IsNullOrEmpty(FuzzyRotorSearch))
                {
                    query = query.Where(t => t.NumberOfJobs.Contains(FuzzyRotorSearch));
                }

                if (!string.IsNullOrEmpty(RotorNumberSearch))
                {
                    query = query.Where(t => t.ProductNumber.Contains(RotorNumberSearch));
                }

                var result = query.ToList();

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }


        public bool Delete(RotorInformation entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;


            return db.SaveChanges() > 0;
        }

        public List<RotorInformation> GetAll()
        {
            return db.RotorInformation.ToList();

        }



        public RotorInformation GetEntity(int id)
        {
            return db.RotorInformation.Find(id);
        }

        public bool Insert(RotorInformation entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Added;
            return db.SaveChanges() > 0;
        }

        public bool Update(RotorInformation entity)
        {
            db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges() > 0;
        }


    }
}
